﻿using CMS.Models;
using CMS.View;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class RecondViewModel:ViewModelBase
    {
        public AppData AppData { get; set; } = AppData.Instance;
        public RecondViewModel()
        {
            Records = new RecordProvider().Select();
        }
        private List<Record> records = new List<Record>();
        public List<Record> Records
        {
            get
            {
                return records;
            }
            set
            {
                records = value;
                RaisePropertyChanged(nameof(Records));
            }
        }
        /// <summary>
        /// 打开入库对话框
        /// </summary>
        public RelayCommand<Record> OpenInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var inputCargoViewModel=ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if(inputCargoViewModel==null)return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if (cargo == null) return;
                  inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }
        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Record> OpenOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var outputCargoViewModel = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoViewModel == null) return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if (cargo == null) return;
                    outputCargoViewModel.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }
        //编辑数据
        public RelayCommand<object> EditRecordCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Record model)) return;
                   // model.CargoId = this.Records.FirstOrDefault(t => t.Id == model.CargoId).Id;
                    //model.MenberId= this.Records.FirstOrDefault(t => t.Id == model.MenberId).Id;
                    var count = new RecordProvider().Update(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");
                    Records = new RecordProvider().Select();//刷新数据
                });
            }
        }
    }
}
